package com.ybkj.o2o.mc.controller.res;

import com.google.common.base.Preconditions;
import com.ybkj.o2o.mc.common.DataGrid;
import com.ybkj.o2o.mc.common.query.AccountQuery;
import com.ybkj.o2o.mc.common.transport.Error;
import com.ybkj.o2o.mc.common.transport.Result;
import com.ybkj.o2o.mc.entity.Department;
import com.ybkj.o2o.mc.entity.Enterprise;
import com.ybkj.o2o.mc.entity.Passenger;
import com.ybkj.o2o.mc.entity.PassengerAccount;
import com.ybkj.o2o.mc.entity.PassengerGrade;
import com.ybkj.o2o.mc.manager.DepartmentManager;
import com.ybkj.o2o.mc.manager.EnterpriseManager;
import com.ybkj.o2o.mc.manager.PassengerAccountManager;
import com.ybkj.o2o.mc.manager.PassengerManager;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "api/passengerAccount")
public class PassengerAccountRes {
  @Autowired
  private PassengerManager passengerManager;
  @Autowired
  private EnterpriseManager enterpriseManager;
  @Autowired
  private DepartmentManager departmentManager;
  @Autowired
  private PassengerAccountManager passengerAccountManager;

  /**
   * 修改账户记录.
   */
  @RequestMapping(value = "{passengerId}/accountDetail/{accountId}", method = RequestMethod.PUT)
  public ResponseEntity<Object> update(
          @PathVariable("accountId") Long accountId,
          @PathVariable("passengerId") Long passengerId,
          @RequestParam("money") Double money,
          @RequestParam("afterDrawMoney") Double afterDrawMoney,
          @RequestParam("afterNoDrawMoney") Double afterNoDrawMoney,
          @RequestParam("afterFrozenMoney") Double afterFrozenMoney,
          @RequestParam("payType") String payType,
          @RequestParam(required = false, value = "details") String details,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    
    //修改
    try {
      PassengerAccount passengerAccount = passengerAccountManager.fetch(accountId, passengerId);
      
      passengerAccount.setMoney(money);
      passengerAccount.setAfterDrawMoney(afterDrawMoney);
      passengerAccount.setAfterFrozenMoney(afterFrozenMoney);
      passengerAccount.setAfterNoDrawMoney(afterNoDrawMoney);
      passengerAccount.setDetails(details);
      passengerAccount.setPayType(payType);
      passengerAccountManager.update(passengerAccount);

      Result<PassengerGrade> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 分页查询乘客账户记录.
   *
   * @param different "Positive">0,"Negative"<=0
   */
  @RequestMapping(value = {"{passengerId}/passengerAccount"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findAccountDetail(
          @PathVariable("passengerId") Long passengerId,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "different") String different,
          @RequestParam(required = false, value = "payType") String payType,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");

    AccountQuery accountQuery = new AccountQuery();
    accountQuery.setPassengerId(passengerId);
    accountQuery.setStartTime(start);
    accountQuery.setEndTime(end);
    accountQuery.setNegative(different);
    accountQuery.setPayType(payType);
    accountQuery.setPage(page);
    accountQuery.setRows(rows);
    accountQuery.setAppKey(appKey);

    return getAccountDetail(accountQuery);

  }

  /**
   * 分页查询企业账户记录.
   *
   * @param different "Positive">0,"Negative"<=0
   */
  @RequestMapping(value = {"{enterpriseId}/accountDetail"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findAccount(
          @PathVariable("enterpriseId") Long enterpriseId,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "different") String different,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Enterprise enterprise = enterpriseManager.findByIdAndAppKey(enterpriseId, appKey);
    Preconditions.checkArgument(enterprise != null, "此企业不存在");

    AccountQuery accountQuery = new AccountQuery();
    accountQuery.setEnterpriseId(enterpriseId);
    accountQuery.setStartTime(start);
    accountQuery.setEndTime(end);
    accountQuery.setNegative(different);
    accountQuery.setPage(page);
    accountQuery.setRows(rows);
    accountQuery.setAppKey(appKey);
    
    return getAccountDetail(accountQuery);

  }
  
  /**
   * 分页查询部门账户记录.
   *
   * @param different "Positive">0,"Negative"<=0
   */
  @RequestMapping(value = {"{departmentId}/departmentAccountDetail"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findDepartmentAccount(
          @PathVariable("departmentId") Long departmentId,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "different") String different,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Department department = departmentManager.findByIdAndAppKey(departmentId, appKey);
    Preconditions.checkArgument(department != null, "此部门不存在");

    AccountQuery accountQuery = new AccountQuery();
    accountQuery.setDepartmentId(departmentId);
    accountQuery.setStartTime(start);
    accountQuery.setEndTime(end);
    accountQuery.setNegative(different);
    accountQuery.setPage(page);
    accountQuery.setRows(rows);
    accountQuery.setAppKey(appKey);
    
    return getAccountDetail(accountQuery);

  }

  /**
   * 多条件分页查询账户记录.
   *
   * @param companyId 公司Id
   * @param start     充值时间开始
   * @param end       充值时间结束
   * @param payType   支付类型(充值方式)
   * @param param     匹配乘客姓名,操作人
   */
  @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findPassengerAccounts(
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "payType") String payType,
          @RequestParam(required = false, value = "param") String param,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    AccountQuery accountQuery = new AccountQuery();
    accountQuery.setCompanyId(companyId);
    accountQuery.setPayType(payType);
    accountQuery.setStartTime(start);
    accountQuery.setEndTime(end);
    accountQuery.setPage(page);
    accountQuery.setRows(rows);
    accountQuery.setKeyword(param);
    accountQuery.setAppKey(appKey);

    return getAccountDetail(accountQuery);

  }

  private ResponseEntity<Object> getAccountDetail(AccountQuery accountQuery) {
    ResponseEntity<Object> responseEntity;
    try {
      DataGrid<PassengerAccount> dataGrid = passengerAccountManager.findAccountDetail(accountQuery);
      Result<DataGrid<PassengerAccount>> result = new Result<>(0, "成功", dataGrid);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);
    	
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }

  /**
   * 根据条件查询账户金额总和.
   *
   * @param companyId 乘客所属公司Id
   * @param start     创建时间开始
   * @param end       创建时间结束
   * @param payType   支付类型
   * @param param     乘客姓名(模糊匹配)
   * @param different "Positive">0,"Negative"<=0
   * @return 返回发生金额sum一个属性值
   */
  @RequestMapping(value = {"sumAccount"}, method = RequestMethod.GET)
  public ResponseEntity<Object> countAccount(
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "passengerId") Long passengerId,
          @RequestParam(required = false, value = "introducerId") Long introducerId,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "payType") String payType,
          @RequestParam(required = false, value = "param") String param,
          @RequestParam(required = false, value = "different") String different,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    AccountQuery accountQuery = new AccountQuery();
    accountQuery.setPassengerId(passengerId);
    accountQuery.setIntroducerId(introducerId);
    accountQuery.setCompanyId(companyId);
    accountQuery.setPayType(payType);
    accountQuery.setNegative(different);
    accountQuery.setStartTime(start);
    accountQuery.setEndTime(end);
    accountQuery.setKeyword(param);
    accountQuery.setAppKey(appKey);
    
    try {
      double sum = passengerAccountManager.sumAccount(accountQuery);
      Result<Double> result = new Result<>(0, "成功", sum);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }

  /**
   * 根据条件查询账户记录.
   *
   * @param orderId 订单Id
   * @param payType 支付类型
   */
  @RequestMapping(value = {"findByOrderId"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findAccountByOrderId(
          @RequestParam(required = false, value = "orderId") Long orderId,
          @RequestParam(required = false, value = "payType") String payType,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {
      List<PassengerAccount> list = passengerAccountManager.findAccountByOrderId(orderId,
              payType, appKey);

      Result<List<PassengerAccount>> result = new Result<>(0, "成功", list);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }

}

